package cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-28
 * Time:2:52
 */
@WebServlet(name = "CreateCookie", urlPatterns = "/createCookie")
public class CreateCookie extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("username", "jf");
        cookie1.setMaxAge(2000);
        cookie1.setPath(req.getContextPath() + "/aaa");
        resp.addCookie(cookie1);
        Cookie cookie2 = new Cookie("email", "jf@gmail.com");
        cookie2.setMaxAge(-1);
        resp.addCookie(cookie2);
        Cookie cookie3 = new Cookie("hello", "world");
        cookie3.setPath(req.getContextPath() + "/aaa");
        resp.addCookie(cookie3);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<h1>成功创建Cookie</h1>");
        writer.flush();
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
